package neusoft.yuejian_11.model;

import java.util.List;

import neusoft.yuejian_11.bean.ActivitynInBean;
import neusoft.yuejian_11.bean.EventBean;
import neusoft.yuejian_11.bean.EventnBean;
import neusoft.yuejian_11.bean.IsEventn;
import neusoft.yuejian_11.bean.IsMatchn;
import neusoft.yuejian_11.interF.Const;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.interF.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by neusoft on 2017/10/9.
 */

public class EventModel {
    private Retrofit retrofit;
    public EventModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //活动列表
    public void eventList(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<EventBean>> call = retrofitService.getAllEvents();
        Callback<List<EventBean>> callback = new Callback<List<EventBean>>() {
            @Override
            public void onResponse(Call<List<EventBean>> call, Response<List<EventBean>> response) {
                List<EventBean> eventBeanList = response.body();
                listener.onSuccess(eventBeanList,1);
            }

            @Override
            public void onFailure(Call<List<EventBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //活动详情
    public void eventDetail(String activityId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call = retrofitService.getEventById(activityId);
        Callback<EventBean> callback = new Callback<EventBean>() {
            @Override
            public void onResponse(Call<EventBean> call, Response<EventBean> response) {
                EventBean eventBeanDetail = response.body();
                listener.onSuccess(eventBeanDetail,1);
            }

            @Override
            public void onFailure(Call<EventBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //活动报名
    public void eventn (int userId, int activityId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<EventnBean> call = retrofitService.getEventnById(userId,activityId);
        Callback<EventnBean> callback = new Callback<EventnBean>() {
            @Override
            public void onResponse(Call<EventnBean> call, Response<EventnBean> response) {
                EventnBean eventnBean = response.body();
                listener.onSuccess(eventnBean,4);
            }

            @Override
            public void onFailure(Call<EventnBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //判断是否报名
    public void IsAddActivityn(int userId, String activityId,final RetrofitListener listener) {
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call=retrofitService.IsAddActivityn(userId,String.valueOf(activityId));
        call.enqueue(new Callback<IsEventn>() {
            @Override
            public void onResponse(Call<IsEventn> call, Response<IsEventn> response) {
                IsEventn isEventn=response.body();
                listener.onSuccess(isEventn,5);
            }
            @Override
            public void onFailure(Call<IsEventn> call, Throwable t) {

            }
        });
    }
    //活动报名列表
    public void eventnInList(int userId,final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<ActivitynInBean>> call = retrofitService.getActivityReg(userId);
        Callback<List<ActivitynInBean>> callback = new Callback<List<ActivitynInBean>>() {
            @Override
            public void onResponse(Call<List<ActivitynInBean>> call, Response<List<ActivitynInBean>> response) {
                List<ActivitynInBean> activitynInBeanList = response.body();
                listener.onSuccess(activitynInBeanList,1);
            }

            @Override
            public void onFailure(Call<List<ActivitynInBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}
